/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.web.controller;

import com.intelligent.ispc.common.dto.JsonResponseDto;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.core.dto.CPWarningRuleDto;
import com.intelligent.ispc.core.dto.ControlChartWarningRuleDto;
import com.intelligent.ispc.core.dto.DataDictionaryDto;
import com.intelligent.ispc.core.service.AlarmService;
import com.intelligent.ispc.core.service.ProjectService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import java.util.List;

/**
 * Created by cherry on 2015/12/9.
 */
@Controller
@RequestMapping(value = "/alarmRule")
public class AlarmRuleController {

    @Autowired
    private AlarmService alarmService;

    @Autowired
    private ProjectService projectService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String nav(Model model, ServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        if(!subject.isPermitted("system:alarmrule:view")) {
            return "error/noPermission";
        }
        return "system/alarmDetail";
    }

    @ResponseBody
    @RequestMapping(value = "/findCCWR", method = RequestMethod.POST)
    public JsonResponseDto findDefaultCCWR(Model model) {

        Subject subject = SecurityUtils.getSubject();
        if(!subject.isPermitted("system:alarmrule:view")) {
            return JsonResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
        }

        List<ControlChartWarningRuleDto> list = projectService.findDefaultControlChartWarningRuleDto();
        JsonResponseDto json = JsonResponseDto.SUCCESS().setBody(list);
        return json;
    }

    @ResponseBody
    @RequestMapping(value = "/findCPWR", method = RequestMethod.POST)
    public JsonResponseDto findDefaultCPWR( Model model) {

        Subject subject = SecurityUtils.getSubject();
        if(!subject.isPermitted("system:alarmrule:view")) {
            return JsonResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
        }
        List<CPWarningRuleDto> list = projectService.findDefaultCPWarningRuleDto();
        JsonResponseDto json = JsonResponseDto.SUCCESS().setBody(list);
        return json;
    }

    @ResponseBody
    @RequestMapping(value = "updateAlarmRuleCPWR", method = RequestMethod.POST)
    public JsonResponseDto updateCPWR(@RequestBody List<DataDictionaryDto> list) {

        Subject subject = SecurityUtils.getSubject();
        if(!subject.isPermitted("system:alarmrule:edit")) {
            return JsonResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
        }

        DataDictionaryDto dataDictionaryDto = list.get(0);

        try {
            alarmService.updateSystemAlarmRuleCPWR(dataDictionaryDto);
        } catch (Exception e) {
            return JsonResponseDto.FAILURE().setMessage(e.getMessage());
        }
        return JsonResponseDto.SUCCESS();
    }

    @ResponseBody
    @RequestMapping(value = "updateAlarmRuleCCWR", method = RequestMethod.POST)
    public JsonResponseDto updateCCWR(@RequestBody List<DataDictionaryDto> list) {

        Subject subject = SecurityUtils.getSubject();
        if(!subject.isPermitted("system:alarmrule:edit")) {
            return JsonResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
        }

        DataDictionaryDto dataDictionaryDto = list.get(0);

        try {
            alarmService.updateSystemAlarmRuleCCWR(dataDictionaryDto);
        } catch (Exception e) {
            return JsonResponseDto.FAILURE().setMessage(e.getMessage());
        }
        return JsonResponseDto.SUCCESS();
    }
}
